package com.ruoyi.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.domain.BatchMessageVO;
import com.ruoyi.common.core.domain.GeTuiResult;
import com.ruoyi.common.core.domain.PushMessageVO;
import com.ruoyi.common.core.domain.SingleMessageVO;
import com.ruoyi.common.core.enums.PushType;
import com.ruoyi.common.core.utils.GeTuiUtils;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.domain.SysUserAccount;
import com.ruoyi.system.service.IMsgPushService;
import com.ruoyi.system.service.ISysUserAccountService;
import com.ruoyi.system.utils.GeTuiConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//import com.sun.org.apache.xerces.internal.xs.StringList;

@Service
@Slf4j
public class MsgPushServiceImpl implements IMsgPushService {

    @Autowired
    private ISysUserAccountService accountService;

    @Autowired
    private RedisService redisService;

    // return 0 用户列表为空，1 成功，-1失败
    @Override
    public int pushToAPP(int pushType, PushMessageVO pushMessageVO, List<String> users) {
        String token = redisService.getCacheObject(GeTuiConfig.appKey);
        BatchMessageVO batchMessageVO = new BatchMessageVO();
        batchMessageVO.setIs_async(true);
        GeTuiResult result = null;
        if (pushType == PushType.SINGLE.getCode()){
            //推送单独用户
            // 根据用户名查询用户信息
            List<SysUserAccount> list = accountService.selectUserAccountByUserList(users);
            if (CollectionUtil.isEmpty(list)){
                return 0;
            }
            //组装数据
            List<SingleMessageVO> pushList = new ArrayList<>();
            list.forEach(o ->{
                SingleMessageVO singleMessageVO = new SingleMessageVO();
                singleMessageVO.setPush_message(pushMessageVO);
                SingleMessageVO.AudienceDTO audienceDTO = new SingleMessageVO.AudienceDTO();
                audienceDTO.setCid(Arrays.asList(o.getCid()));
                singleMessageVO.setAudience(audienceDTO);
                pushList.add(singleMessageVO);
            });
            //推送

            batchMessageVO.setMsg_list(pushList);
            result = GeTuiUtils.singlePush(GeTuiConfig.baseUrl, token, batchMessageVO.getMsg_list().get(0),0);

        }else if (pushType == PushType.ALIAS.getCode()){
            //根据标签推送
            List<SysUserAccount> list = accountService.selectUserAccountByAliasList(users);
            if (CollectionUtil.isEmpty(list)){
                return 0;
            }
            List<SingleMessageVO> pushList = new ArrayList<>();
            list.forEach(o ->{
                SingleMessageVO singleMessageVO = new SingleMessageVO();
                singleMessageVO.setPush_message(pushMessageVO);
                SingleMessageVO.AudienceDTO audienceDTO = new SingleMessageVO.AudienceDTO();
                audienceDTO.setAlias(Arrays.asList(o.getAlias()));
                singleMessageVO.setAudience(audienceDTO);
                pushList.add(singleMessageVO);
            });
            //推送
            batchMessageVO.setMsg_list(pushList);
            result = GeTuiUtils.batchPush(GeTuiConfig.baseUrl, token, batchMessageVO,1);
        }else {
            //推送所有人
            List<SysUserAccount> list = accountService.selectSysUserAccountList(new SysUserAccount());
            if (CollectionUtil.isEmpty(list)){
                return 0;
            }
            List<SingleMessageVO> pushList = new ArrayList<>();
            list.forEach(o ->{
                SingleMessageVO singleMessageVO = new SingleMessageVO();
                singleMessageVO.setPush_message(pushMessageVO);
                SingleMessageVO.AudienceDTO audienceDTO = new SingleMessageVO.AudienceDTO();
                audienceDTO.setCid(Arrays.asList(o.getCid()));
                singleMessageVO.setAudience(audienceDTO);
                pushList.add(singleMessageVO);
            });
            //推送
            batchMessageVO.setMsg_list(pushList);
            result = GeTuiUtils.batchPush(GeTuiConfig.baseUrl, token, batchMessageVO,1);
        }

        if (result.getCode() == 0){
            // 推送成功
            log.info("====>推送成功");
            return 1;
        }else {
            // 推送失败
            log.error("====>推送失败::" + result.getMsg());
            return -1;
        }
    }

    @Override
    public void pushToWX() {

    }

    @Override
    public void pushToSystem() {

    }
}
